package com.lanjiu.lib.imclient.dto.chat;

import android.text.TextUtils;

import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.protobuf.ByteString;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.utils.log.Logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description: 语音
 * @author: Panwei
 * @date: 2019/11/14 21:14
 */
public class VoiceDto extends Dto {

    // 语音
    private String content;

    // 当前坐标
    private long pos;

    // 当前文件内容大小
    private long length;

    public VoiceDto() {
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getPos() {
        return pos;
    }

    public void setPos(long pos) {
        this.pos = pos;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public VoiceDto(BusinessProtocolEntities.Voice voice, String content) {
        this.content = content;
        this.pos = voice.getPos();
        this.length = voice.getLength();
    }

    @Override
    public BusinessProtocolEntities.Voice toProtobuf() {
        BusinessProtocolEntities.Voice.Builder builder = BusinessProtocolEntities.Voice.newBuilder();
        if (!TextUtils.isEmpty(content)) {
            try {
                InputStream is = new FileInputStream(content);
                builder.setContent(ByteString.readFrom(is));
                is.close();
            } catch (IOException e) {
                Logger.print("Exception", e);
            }
        }
        return builder.setPos(pos)
                .setLength(length)
                .build();
    }

    @Override
    public String toJson() {
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(this);
    }
}
